package com.glory.hikvisionmobile.adapter;

import android.util.SparseBooleanArray;
import android.widget.CheckBox;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;

import java.util.ArrayList;
import java.util.List;

public class TrackInItemWorkAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    private SparseBooleanArray mCheckStates = new SparseBooleanArray();
    private List<String> mData = new ArrayList<>();

    public TrackInItemWorkAdapter(@Nullable List<String> data) {
        super(R.layout.adapter_item_work_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.tvItemWork,item);
        CheckBox checkbox = helper.getView(R.id.checkbox);
        checkbox.setTag(helper.getAdapterPosition());
        checkbox.setChecked(mCheckStates.get(helper.getAdapterPosition(), false));
        checkbox.setOnClickListener(v -> {
            boolean isChecked = ((CheckBox) v).isChecked();
            int pos = (int) v.getTag();
            if (isChecked) {
                mCheckStates.put(pos, true);
                mData.add(item);
            } else {
                mCheckStates.put(pos, false);
                mData.remove(item);
            }
        });
    }

    public String getItemWork(){
        StringBuffer sb = new StringBuffer();
        if (mData.size() > 0){
                for (String s : mData){
                    sb.append(s + ";");
            }
        }
        return sb.toString();
    }

    public void clear(){
        mData.clear();
        mCheckStates.clear();
    }

}
